home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / T U R B O Language / Turbo Pascal V7.0 / DOCDEMO.ZIP / COLLECT1.PAS next >
Encoding:
Pascal/Delphi Source File  |  1992-10-30  |  2.5 KB  |  108 lines

  1. {************************************************}
  2. {                                                }
  3. {   Turbo Vision 2.0 Demo                        }
  4. {   Copyright (c) 1992 by Borland International  }
  5. {                                                }
  6. {************************************************}
  7.  
  8. program Collect1;
  9.  
  10. uses Objects;
  11.  
  12. type
  13.   PClient = ^TClient;
  14.   TClient = object(TObject)
  15.     Account, Name, Phone: PString;
  16.     constructor Init(NewAccount, NewName, NewPhone: String);
  17.     destructor Done; virtual;
  18.     procedure Print; virtual;
  19.   end;
  20.  
  21. { TClient }
  22. constructor TClient.Init(NewAccount, NewName, NewPhone: String);
  23. begin
  24.   Account := NewStr(NewAccount);
  25.   Name := NewStr(NewName);
  26.   Phone := NewStr(NewPhone);
  27. end;
  28.  
  29. destructor TClient.Done;
  30. begin
  31.   DisposeStr(Account);
  32.   DisposeStr(Name);
  33.   DisposeStr(Phone);
  34. end;
  35.  
  36. procedure TClient.Print;
  37. begin
  38.   Writeln('  ',
  39.     Account^, '':10-Length(Account^),
  40.     Name^, '':20-Length(Name^),
  41.     Phone^, '':16-Length(Phone^));
  42. end;
  43.  
  44. { Use ForEach iterator to display client information }
  45.  
  46. procedure PrintAll(C: PCollection);
  47.  
  48. procedure CallPrint(P : PClient); far;
  49. begin
  50.   P^.Print;                   { Call Print method }
  51. end;
  52.  
  53. begin { Print }
  54.   Writeln;
  55.   Writeln;
  56.   Writeln('Client list:');
  57.   C^.ForEach(@CallPrint);     { Print each client }
  58. end;
  59.  
  60. { Use FirstThat iterator to search non-key field }
  61.  
  62. procedure SearchPhone(C: PCollection; PhoneToFind: String);
  63.  
  64. function PhoneMatch(Client: PClient): Boolean; far;
  65. begin
  66.   PhoneMatch := Pos(PhoneToFind, Client^.Phone^) <> 0;
  67. end;
  68.  
  69. var
  70.   FoundClient: PClient;
  71.  
  72. begin { SearchPhone }
  73.   Writeln;
  74.   FoundClient := C^.FirstThat(@PhoneMatch);
  75.   if FoundClient = nil then
  76.     Writeln('No client met the search requirement')
  77.   else
  78.   begin
  79.     Writeln('Found client:');
  80.     FoundClient^.Print;
  81.   end;
  82. end;
  83.  
  84. var
  85.   ClientList: PCollection;
  86.  
  87. begin
  88.   ClientList := New(PCollection, Init(10, 5));
  89.  
  90.   { Build collection }
  91.   with ClientList^ do
  92.   begin
  93.     Insert(New(PClient, Init('91-100', 'Anders, Smitty', '(406) 111-2222')));
  94.     Insert(New(PClient, Init('90-167', 'Smith, Zelda', '(800) 555-1212')));
  95.     Insert(New(PClient, Init('90-177', 'Smitty, John', '(406) 987-4321')));
  96.     Insert(New(PClient, Init('90-160', 'Johnson, Agatha', '(302) 139-8913')));
  97.   end;
  98.  
  99.   { Use ForEach iterator to print all }
  100.   PrintAll(ClientList);
  101.  
  102.   { Use FirstThat iterator to find match with search pattern }
  103.   SearchPhone(ClientList, '(406)');
  104.  
  105.   { Clean up }
  106.   Dispose(ClientList, Done);
  107. end.
  108.